package com.yuer.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.txn.CreateSessionTxn;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZkTest implements Watcher {

    static CountDownLatch countDownLatch = new CountDownLatch(1);
    private  static ZooKeeper zooKeeper;
    public static void main(String[] args) {
        try {
              zooKeeper =  new ZooKeeper("192.168.56.103:2182",5000,new ZkTest());
            System.out.println("此时的链接状态：  "+zooKeeper.getState());
            countDownLatch.await();
            System.out.println("此时的链接状态---->：  "+zooKeeper.getState());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("方法执行中....."+event.getState());
        if(event.getState() == Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
        }
    }
}
